<?php

/**
 * This is the model class for table "{{category}}".
 *
 * The followings are the available columns in table '{{category}}':
 * @property integer $zca_id
 * @property integer $zta_id
 * @property integer $zca_topid
 * @property integer $zca_parentid
 * @property integer $zca_childid
 * @property string $zca_arrchildid
 * @property string $zca_title
 * @property string $zca_url
 * @property string $zca_aliasname
 * @property integer $zca_highlight
 * @property integer $zca_level
 * @property string $zca_seotitle
 * @property string $zca_seokey
 * @property string $zca_seodesc
 * @property string $zca_desc
 * @property integer $zca_visible
 * @property string $zca_taoid
 * @property integer $zca_listorder
 * @property integer $inputtime
 * @property integer $updatetime
 */
class Category extends CActiveRecord
{
	/**
	 * Returns the static model of the specified AR class.
	 * @param string $className active record class name.
	 * @return Category the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return '{{category}}';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('zta_id, zca_topid, zca_parentid, zca_childid, zca_highlight, zca_level, zca_visible, zca_listorder, inputtime, updatetime', 'numerical', 'integerOnly'=>true),
			array('zca_title, zca_aliasname, zca_seotitle', 'length', 'max'=>100),
			array('zca_url, zca_seokey, zca_seodesc', 'length', 'max'=>255),
			array('zca_taoid', 'length', 'max'=>50),
			array('zca_arrchildid, zca_desc', 'safe'),
			array('zca_id, zta_id, zca_topid, zca_parentid, zca_childid, zca_arrchildid, zca_title, zca_url, zca_aliasname, zca_highlight, zca_level, zca_seotitle, zca_seokey, zca_seodesc, zca_desc, zca_visible, zca_taoid, zca_listorder, inputtime, updatetime', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		return array(
				'attachment'=>array(self::BELONGS_TO, 'Attachment', 'zta_id'),
				'attmod'=>array(self::BELONGS_TO, 'AttMod', 'zmd_id'),
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'zca_id' => '行业分类ID',
			'zta_id' => '附件ID(标志图片)',
			'zca_topid' => '顶级Id',
			'zca_parentid' => '上级ID',
			'zca_childid' => '是否有子ID',
			'zca_arrchildid' => '子ID',
			'zca_title' => '分类标题',
			'zca_url' => '分类URl',
			'zca_aliasname' => '分类别名',
			'zca_highlight' => '是否高亮',
			'zca_level' => '等级',
			'zca_seotitle' => 'SEO标题',
			'zca_seokey' => 'SEO关键词',
			'zca_seodesc' => 'SEO描述',
			'zca_desc' => '说明',
			'zca_visible' => '是否显示',
			'zca_taoid' => '淘宝分类ID',
			'zca_listorder' => '排序',
			'inputtime' => '创建时间',
			'updatetime' => '更新时间',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('zca_id',$this->zca_id);
		$criteria->compare('zta_id',$this->zta_id);
		$criteria->compare('zca_topid',$this->zca_topid);
		$criteria->compare('zca_parentid',$this->zca_parentid);
		$criteria->compare('zca_childid',$this->zca_childid);
		$criteria->compare('zca_arrchildid',$this->zca_arrchildid,true);
		$criteria->compare('zca_title',$this->zca_title,true);
		$criteria->compare('zca_url',$this->zca_url,true);
		$criteria->compare('zca_aliasname',$this->zca_aliasname,true);
		$criteria->compare('zca_highlight',$this->zca_highlight);
		$criteria->compare('zca_level',$this->zca_level);
		$criteria->compare('zca_seotitle',$this->zca_seotitle,true);
		$criteria->compare('zca_seokey',$this->zca_seokey,true);
		$criteria->compare('zca_seodesc',$this->zca_seodesc,true);
		$criteria->compare('zca_desc',$this->zca_desc,true);
		$criteria->compare('zca_visible',$this->zca_visible);
		$criteria->compare('zca_taoid',$this->zca_taoid,true);
		$criteria->compare('zca_listorder',$this->zca_listorder);
		$criteria->compare('inputtime',$this->inputtime);
		$criteria->compare('updatetime',$this->updatetime);
		//排序
		$criteria->order = "zca_listorder ASC, zca_id ASC ";
		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
			'pagination'=>array(
					'pageSize'=>1000,
			)
		));
	}

	/*
	 * 返回一级菜单列表
	 * */
	public function loadTop(){
		$criteria=new CDbCriteria;
		$criteria->compare('zca_level','1');
		$criteria->order = 'zca_listorder ASC , zca_id ASC';
		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
		));
	}
	
	/*
	 * 返回一级菜单列表
	* */
	public function loadTopList($type=null){
		if($type==null){
			$criteria=new CDbCriteria;
			$criteria->compare('zca_level','1');
			$criteria->order = 'zca_listorder ASC , zca_id ASC ';
			$catetop = $this->findAll($criteria);
			return CHtml::listData($catetop,'zca_id','zca_title');
		}else{
			$data = $this->loadTopList();
			if(array_key_exists($type,$data))
				return $data[$type];
		}
	}

	/*
	 * 返回子菜单列表
	* */
	public function loadChild($parentid){
		$criteria=new CDbCriteria;
		$criteria->compare('zca_parentid',$parentid);
		$criteria->order = 'zca_listorder ASC, zca_id DESC ';
		return new CActiveDataProvider($this, array(
				'criteria'=>$criteria,
		));
	}

	/*
	 * 自动收纳子分类ID
	 * */
	public function acceptChild(){
		//更新二能让分类子ID
		$criteria=new CDbCriteria;
		$criteria->compare('zca_level','2');
		$criteria->select = 'zca_id';
		$sedItem = $this->findAll($criteria);
		if(!empty($sedItem)){
			$sedItem = simplifyData($sedItem);
			foreach($sedItem as $valSed){
				$criteria=new CDbCriteria;
				$criteria->compare('zca_parentid',$valSed['zca_id']);
				$criteria->select = 'zca_id';
				$childs=$this->findAll($criteria);
				if(!empty($childs)){
					$childs = simplifyData($childs);
					$model = $this->findByPk($valSed['zca_id']);
					$model->zca_childid = '2';
					$model->zca_arrchildid = "";
					foreach($childs as $valThr){
						if($model->zca_arrchildid==""){
							$model->zca_arrchildid = $valThr['zca_id'];
						}else{
							$model->zca_arrchildid .=','.$valThr['zca_id'];
						}
					}
					$model->save();
				}
			}
		}
		//更新一级分类子ID
		$criteria=new CDbCriteria;
		$criteria->compare('zca_level','1');
		$oneItem = $this->findAll($criteria);
		if(!empty($oneItem)){
			$oneItem = simplifyData($oneItem);
			foreach($oneItem as $val){
				$criteria=new CDbCriteria;
				$criteria->compare('zca_parentid',$val['zca_id']);
				$criteria->select = 'zca_id,zca_arrchildid,zca_childid';
				$childs=$this->findAll($criteria);
				if(!empty($childs)){
					$childs = simplifyData($childs);
					$model = $this->findByPk($val['zca_id']);
					$model->zca_childid = '2';
					$model->zca_arrchildid = "";
					foreach($childs as $valSed){
						if($model->zca_arrchildid==""){
							$model->zca_arrchildid = $valSed['zca_id'];
							if($valSed['zca_childid']=='2'){
								$model->zca_arrchildid .=','.$valSed['zca_arrchildid'];
							}
						}else{
							$model->zca_arrchildid .=','.$valSed['zca_id'];
							if($valSed['zca_childid']=='2'){
								$model->zca_arrchildid .=','.$valSed['zca_arrchildid'];
							}
						}
					}
					$model->save();
				}
			}
			return true;
		}
	}

	/*
	 * 根据分类ID生成分类名称
	 * */
	public function getTitle($id){
		$criteria=new CDbCriteria;
		$criteria->compare('zca_id',$id);
		$category = $this->find($criteria);
		if(!empty($category)){
			return $category->zca_title;
		}else{
			return "";
		}
	}

	/*
	 * 根据父级id查找分类  
	 * $parentid 父级分类id
	 * $data 返回一个数组集合*/
	public function GetCatByParentid($parentid){
		$sql =  'select zca_id,
				          zca_parentid,
    					  zca_title,
    					  zca_topid,
    					  zca_url,
				          zca_visible,
				          zca_highlight,
				          zca_childid,
				          zca_arrchildid
    			   from {{category}} where zca_parentid='.$parentid;
		$data = Yii::app()->db
		->createCommand($sql)
		->queryAll();
		return $data;
	}	
	
	/*
	 * 建立分类缓存
	 */
	public function GetCatCache(){
		$data = json_decode(Yii::app()->filecache->get('category_cache'),true);//获取缓存
		//如果缓存不存在，则直接读取数据库

		if(empty($data)){			
			//获取第一级分类
			$data_one = self::GetCatByParentid('0');			
			foreach ($data_one as $key=>$val){
				$data_ones[$val['zca_id']]['zca_id'] =$val['zca_id'];
				$data_ones[$val['zca_id']]['zca_parentid'] =$val['zca_parentid'];
				$data_ones[$val['zca_id']]['zca_title'] =$val['zca_title'];
				$data_ones[$val['zca_id']]['zca_topid'] =$val['zca_topid'];
				$data_ones[$val['zca_id']]['zca_url'] =$val['zca_url'];
				$data_ones[$val['zca_id']]['zca_visible'] =$val['zca_visible'];
				$data_ones[$val['zca_id']]['zca_highlight'] =$val['zca_highlight'];
				$data_ones[$val['zca_id']]['zca_arrchildid'] =$val['zca_arrchildid'];

				//一级分类下面的产品数量
				if($val['zca_childid']==2){	
					$data_ones[$val['zca_id']]['pro_count'] = Product::model()->count('zca_id in('.$val['zca_arrchildid'].') AND zp_status = 2 AND zp_state = 2');
				}else{
					$data_ones[$val['zca_id']]['pro_count'] = Product::model()->count('zca_id in('.$val['zca_id'].') AND zp_status = 2 AND zp_state = 2');
				}
				//一级分类下面的产品数量
				$data_two= self::GetCatByParentid($val['zca_id']);
				foreach($data_two as $key1=>$val1){
					$data_ones[$val['zca_id']]['subcat'][$val1['zca_id']]['zca_id'] =$val1['zca_id'];
					$data_ones[$val['zca_id']]['subcat'][$val1['zca_id']]['zca_parentid'] =$val1['zca_parentid'];
					$data_ones[$val['zca_id']]['subcat'][$val1['zca_id']]['zca_title'] =$val1['zca_title'];
					$data_ones[$val['zca_id']]['subcat'][$val1['zca_id']]['zca_topid'] =$val1['zca_topid'];
					$data_ones[$val['zca_id']]['subcat'][$val1['zca_id']]['zca_url'] =$val1['zca_id'];
					$data_ones[$val['zca_id']]['subcat'][$val1['zca_id']]['zca_visible'] =$val1['zca_visible'];
					$data_ones[$val['zca_id']]['subcat'][$val1['zca_id']]['zca_highlight'] =$val1['zca_highlight'];
					$data_ones[$val['zca_id']]['subcat'][$val1['zca_id']]['zca_arrchildid'] =$val1['zca_arrchildid'];
					//二级分类下面的产品数量
					if($val1['zca_childid']==2){	
						$data_ones[$val['zca_id']]['subcat'][$val1['zca_id']]['pro_count'] =Product::model()->count('zca_id in('.$val1['zca_arrchildid'].') AND zp_status = 2 AND zp_state = 2');
					}else{
						$data_ones[$val['zca_id']]['subcat'][$val1['zca_id']]['pro_count'] =Product::model()->count('zca_id in('.$val1['zca_id'].') AND zp_status = 2 AND zp_state = 2');
					}
					//二级分类下面的分类集合
					$data_three= self::GetCatByParentid($val1['zca_id']);
					foreach($data_three as $key2=>$val2){
						$data_ones[$val['zca_id']]['subcat'][$val1['zca_id']]['subcat'][$val2['zca_id']]['zca_id'] =$val2['zca_id'];
						$data_ones[$val['zca_id']]['subcat'][$val1['zca_id']]['subcat'][$val2['zca_id']]['zca_parentid'] =$val2['zca_parentid'];
						$data_ones[$val['zca_id']]['subcat'][$val1['zca_id']]['subcat'][$val2['zca_id']]['zca_title'] =$val2['zca_title'];
						$data_ones[$val['zca_id']]['subcat'][$val1['zca_id']]['subcat'][$val2['zca_id']]['zca_topid'] =$val2['zca_topid'];
						$data_ones[$val['zca_id']]['subcat'][$val1['zca_id']]['subcat'][$val2['zca_id']]['zca_url'] =$val2['zca_url'];
						$data_ones[$val['zca_id']]['subcat'][$val1['zca_id']]['subcat'][$val2['zca_id']]['zca_visible'] =$val2['zca_visible'];
						$data_ones[$val['zca_id']]['subcat'][$val1['zca_id']]['subcat'][$val2['zca_id']]['zca_highlight'] =$val2['zca_highlight'];
						$data_ones[$val['zca_id']]['subcat'][$val1['zca_id']]['subcat'][$val2['zca_id']]['zca_arrchildid'] =$val2['zca_arrchildid'];
						//三级分类下面的产品数量						
						$data_ones[$val['zca_id']]['subcat'][$val1['zca_id']]['subcat'][$val2['zca_id']]['pro_count'] =Product::model()->count('zca_id ='.$val2['zca_id'].' AND zp_status = 2 AND zp_state = 2');
					}
				}				
			}
			//第一级分类数量
			/* $data_one_count = count($data_one);
			for ($i=0;$i<$data_one_count;$i++){
				//查询一级分类下面的产品数量
				$data_one[$i]['pro_count'] = Product::model()->count('zca_id in('.$data_one[$i]['zca_arrchildid'].')');
    		    //print_r($data_one);
				//查询一级分类下面的分类数组集合
				$data_one[$i]['subcat'] = self::GetCatByParentid($data_one[$i]['zca_id']);
				//第二级分类数量
				$data_two_count = count($data_one[$i]['subcat']);
				for($j=0;$j<$data_two_count;$j++){	
					//查询二级分类下面的产品数量
					$data_one[$i]['subcat'][$j]['pro_count'] = Product::model()->count('zca_id in('.$data_one[$i]['subcat'][$j]['zca_arrchildid'].')');
					//查询二级分类下面的分类数组集合
					$data_one[$i]['subcat'][$j]['subcat']=self::GetCatByParentid($data_one[$i]['subcat'][$j]['zca_id']);
					$data_three_count = count($data_one[$i]['subcat'][$j]['subcat']);
					for($n=0;$n<$data_three_count;$n++){
						//查询三级分类下面的产品数量
						$data_one[$i]['subcat'][$j]['subcat'][$n]['pro_count']=Product::model()
						->count('zca_id ='.$data_one[$i]['subcat'][$j]['subcat'][$n]['zca_id']);
					}
				}
			} */
			//设置缓存
			Yii::app()->filecache->set('category_cache',json_encode($data_ones),3600*24);
			return $data_ones;		   
		}else{
			return $data;
		}		
	}

	/*
	 * 建立ARRAY分类缓存
	 */
	public function GetCategorys(){
		$data = json_decode(Yii::app()->filecache->get('categorys'),true);//获取缓存

		//如果缓存不存在，则直接读取数据库		
		if(empty($data)){
			//获取分类
			Category::model()->acceptChild();
			$data_ones = array();
			$sql =  'select zca_id,
						  zca_topid,
				          zca_parentid,
    					  zca_title,
    					  zca_aliasname,
    					  zca_seotitle,
    					  zca_seokey,
    					  zca_seodesc,
    					  zca_url,
				          zca_visible,
				          zca_highlight,
				          zca_level,
				          zca_childid,
				          zca_arrchildid,
				          zca_listorder
    			   from {{category}} where zca_id!=0';
			$data = Yii::app()->db
			->createCommand($sql)
			->queryAll();
			//print_r($data_ones);exit;
			foreach ($data as $key=>$val)
			{
				$data_ones[$val['zca_id']]['zca_id'] =$val['zca_id'];
				$data_ones[$val['zca_id']]['zca_topid'] =$val['zca_topid'];
				$data_ones[$val['zca_id']]['zca_parentid'] =$val['zca_parentid'];
				$data_ones[$val['zca_id']]['zca_title'] =$val['zca_title'];
				$data_ones[$val['zca_id']]['zca_aliasname'] =$val['zca_aliasname'];
				$data_ones[$val['zca_id']]['zca_seotitle'] =$val['zca_seotitle'];
				$data_ones[$val['zca_id']]['zca_seokey'] =$val['zca_seokey'];
				$data_ones[$val['zca_id']]['zca_seodesc'] =$val['zca_seodesc'];				
				$data_ones[$val['zca_id']]['zca_url'] =$val['zca_url'];
				$data_ones[$val['zca_id']]['zca_visible'] =$val['zca_visible'];
				$data_ones[$val['zca_id']]['zca_highlight'] =$val['zca_highlight'];
				$data_ones[$val['zca_id']]['zca_childid'] =$val['zca_childid'];
				$data_ones[$val['zca_id']]['zca_arrchildid'] =$val['zca_arrchildid'];
				$data_ones[$val['zca_id']]['zca_listorder'] =$val['zca_listorder'];
				$data_ones[$val['zca_id']]['zca_level'] =$val['zca_level'];
				if($val['zca_childid']==2){				
					$data_ones[$val['zca_id']]['pro_count'] = Product::model()->count('zca_id in('.$val['zca_arrchildid'].') AND zp_status = 2 AND zp_state = 2');
				}else{

					$data_ones[$val['zca_id']]['pro_count'] = Product::model()->count('zca_id in('.$val['zca_id'].') AND zp_status = 2 AND zp_state = 2');
				}				
			}								
			//设置缓存
			Yii::app()->filecache->set('categorys',json_encode($data_ones),3600*24);
			return $data_ones;		   
		}else{
			return $data;
		}		
	}

	/**
	 * 一组分类缓存
	 */
	public function getTopCache(){
		$data = Yii::app()->filecache->get('category_top');//获取缓存
		if(!empty($data)){
			return $data;
		}
		
		$criteria=new CDbCriteria;
		$criteria->compare('zca_level','1');
		$criteria->order = 'zca_listorder ASC';
		$data = Category::model()->findAll($criteria);
		Yii::app()->filecache->set('category_top',$data,3600*24);
		return $data;
	}
	
	public function DelCatcache(){
		Yii::app()->filecache->delete('category');
		Yii::app()->filecache->delete('category_cache');
		Yii::app()->filecache->delete('categorys');
		Yii::app()->filecache->delete('category_top');
	}
	
	function afterSave(){
		//清空缓存
		Category::model()->DelCatcache();
		return true;
	}
	
	function afterDelete(){
		//清空缓存
		Category::model()->DelCatcache();
		return true;
	}
	
}


